package com.ymatou.iapi.fastturnover.testcase;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.fastturnover.parameter.VerifyApplyBillReq;
import com.ymatou.iapi.fastturnover.service.VerifyApplyBillCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.database.model.fastturnover.TurnoverApply;
import com.ymttest.database.model.fastturnover.TurnoverApplyDetail;
import com.ymttest.database.sqlwapper.fastturnover.TurnoverApplyDetailWrapper;
import com.ymttest.database.sqlwapper.fastturnover.TurnoverApplyWrapper;
import com.ymttest.utils.TestDataManager;

/**
 * 取消订单 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_VerifyApplyBill {

	private static VerifyApplyBillReq verifyApplyBillReq;
	private static VerifyApplyBillCall verifyApplyBillCall;
	
	private static TurnoverApplyWrapper turnoverApplyWrapper = new TurnoverApplyWrapper();
	private static TurnoverApplyDetailWrapper turnoverApplyDetailWrapper = new TurnoverApplyDetailWrapper();

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("审核提现申请单");
	}

	@Before
	public void caseUp() {
		verifyApplyBillReq = new VerifyApplyBillReq();
		verifyApplyBillCall = new VerifyApplyBillCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(verifyApplyBillCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	public void verify() throws InterruptedException {

		boolean agress = verifyApplyBillReq.isAgree();
		
		for (int i = 0; i < verifyApplyBillReq.getApplyNoList().size(); i++) {
			
			String applyNo = verifyApplyBillReq.getApplyNoList().get(i);
			
			TurnoverApply actualTurnoverApply = turnoverApplyWrapper
					.selectByPrimaryKey(applyNo);
			
			Logger.verifyEquals(agress ? 0 : 2,
					actualTurnoverApply.getPay_status(), "验证pay_status");
			Logger.verifyEquals(agress ? 0 : 3,
					actualTurnoverApply.getRepay_status(), "验证repay_status");
			
			for (TurnoverApplyDetail turnoverApplyDetail : turnoverApplyDetailWrapper.selectByApplyNo(applyNo)) {
				
				Logger.verifyEquals(verifyApplyBillReq.getMemo(),
						turnoverApplyDetail.getMemo(), "验证memo");
				Logger.verifyEquals(agress ? 3 : 2,
						turnoverApplyDetail.getStatus(), "验证status");
			}
			
			if (agress)
				turnoverApplyWrapper.updateToFinishByApplyNo(applyNo);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_VerifyApplyBill_001() {
		Logger.start(true, "审核单个申请单");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			
			String applyNo = "FKT2020040300000022";
			
			turnoverApplyWrapper.updateToInitByApplyNo(applyNo);
			turnoverApplyDetailWrapper.updateToInitByApplyNo(applyNo);
			
			verifyApplyBillReq.setApplyNoList(Arrays.asList(applyNo));
			verifyApplyBillReq.setAgree(true);
			verifyApplyBillReq.setMemo(TestDataManager.randomStr(10));

			// 发送请求
			verifyApplyBillCall.setData(verifyApplyBillReq);
			verifyApplyBillCall.callService();
			
			Logger.verifyEquals(200, verifyApplyBillCall.getRetultCode(),
					"验证Code");

			// 数据库验证
			verify();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_VerifyApplyBill_002() {
		Logger.start(true, "审核多个申请单");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			
			List<String> applyNoList = new ArrayList<>();

			applyNoList.add("FKT2020040300000022");
			applyNoList.add("FKT2020040300000023");
			
			turnoverApplyWrapper.updateToInitByApplyNo("FKT2020040300000022");
			turnoverApplyDetailWrapper.updateToInitByApplyNo("FKT2020040300000022");
			
			turnoverApplyWrapper.updateToInitByApplyNo("FKT2020040300000023");
			turnoverApplyDetailWrapper.updateToInitByApplyNo("FKT2020040300000023");
			
			verifyApplyBillReq.setApplyNoList(applyNoList);
			verifyApplyBillReq.setAgree(true);
			verifyApplyBillReq.setMemo(TestDataManager.randomStr(10));

			// 发送请求
			verifyApplyBillCall.setData(verifyApplyBillReq);
			verifyApplyBillCall.callService();

			Logger.verifyEquals(200, verifyApplyBillCall.getRetultCode(),
					"验证Code");

			// 数据库验证
			verify();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_VerifyApplyBill_003() {
		Logger.start(true, "审核多个申请单， 拒绝");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			
			List<String> applyNoList = new ArrayList<>();

			applyNoList.add("FKT2020040300000022");
			applyNoList.add("FKT2020040300000023");
			
			turnoverApplyWrapper.updateToInitByApplyNo("FKT2020040300000022");
			turnoverApplyDetailWrapper.updateToInitByApplyNo("FKT2020040300000022");
			
			turnoverApplyWrapper.updateToInitByApplyNo("FKT2020040300000023");
			turnoverApplyDetailWrapper.updateToInitByApplyNo("FKT2020040300000023");
			
			verifyApplyBillReq.setApplyNoList(applyNoList);
			verifyApplyBillReq.setAgree(false);
			verifyApplyBillReq.setMemo(TestDataManager.randomStr(10));

			// 发送请求
			verifyApplyBillCall.setData(verifyApplyBillReq);
			verifyApplyBillCall.callService();
			
			Logger.verifyEquals(200, verifyApplyBillCall.getRetultCode(),
					"验证Code");

			// 数据库验证
			verify();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}

